package comm

import (
	"d-serv-go/server/core/bean"
	"d-serv-go/server/core/cache"
	"d-serv-go/server/core/db"
	"d-serv-go/server/core/serv"
	"fmt"
	"github.com/bitly/go-simplejson"
)

type AppServ struct {
	serv.CommServ
}

const (
	APP_VIEW_JSON         = "APP_VIEW_JSON"
	CACHE_SERV_USER_STATE = "CACHE_SERV_USER_STATE"
)

func (a *AppServ) GetViewJson(paramBean *db.ParamBean) *bean.OutBean {
	outBean := bean.NewOutBean()
	viewId := paramBean.GetStr("viewId")
	if jsonObj, ok := cache.CacheMgr.Get(viewId, APP_VIEW_JSON).(string); ok {
		// 根据不同的区域获取视图 从cache里获取视图的json 返回数据
		json, err := simplejson.NewJson([]byte(jsonObj))
		if err != nil {
			outBean.Set("DATASET", json)
		}
	}
	return outBean.SetOk("")
}

func (a *AppServ) GetServJson(paramBean *db.ParamBean) *bean.OutBean {
	outBean := bean.NewOutBean()
	servId := paramBean.GetStr("servId")
	servDef, err := serv.ServUtils.GetServBean(servId)
	if err != nil {
		outBean.SetError(fmt.Sprintf("没有找到服务，异常：%v", err))
	} else {
		// 返回数据
		outBean.Set("DATASET", servDef)
	}
	return outBean.SetOk("")
}
